package com.letoo.dragon.common.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Collection;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

import com.letoo.dragon.common.exception.BusinessException;

/**
 * 业务判断的断言。如果不符合条件会抛出业务异常
 * 
 * @author dengjie
 *
 */
public class AssertUtil {
    /**
     * Assert that an object is not {@code null} .
     * 
     * <pre class="code">
     * Assert.notNull(clazz, &quot;The class must not be null&quot;);
     * </pre>
     * 
     * @param object
     *            the object to check
     * @param message
     *            the exception message to use if the assertion fails
     * @throws IllegalArgumentException
     *             if the object is {@code null}
     */
    public static void notNull(Object object, String message) {
        if (object == null) {
            throw new BusinessException(message);
        }
    }

    public static void isNotBlank(String str, String message) {
        if (StringUtils.isBlank(str)) {
            throw new BusinessException(message);
        }
    }

    /**
     * Assert a boolean expression, throwing {@code IllegalArgumentException} if the test result is {@code false}.
     * 
     * <pre class="code">
     * Assert.isTrue(i &gt; 0, &quot;The value must be greater than zero&quot;);
     * </pre>
     * 
     * @param expression
     *            a boolean expression
     * @param message
     *            the exception message to use if the assertion fails
     * @throws IllegalArgumentException
     *             if expression is {@code false}
     */
    public static void isTrue(boolean expression, String message) {
        if (!expression) {
            throw new BusinessException(message);
        }
    }

    /**
     * 判断集合是否为空
     * 
     * @param object
     * @param message
     */
    public static void notEmpty(Collection<?> object, String message) {
        if (object == null || object.isEmpty()) {
            throw new BusinessException(message);
        }
    }

    /**
     * 判断是否是空字符串
     * 
     * @param str
     * @param message
     */
    public static void notEmpty(String str, String message) {
        if (str == null || str == "") {
            throw new BusinessException(message);
        }
    }

    /**
     * 断言是否是正确的日期格式，否在抛出Bussiness异常
     * 
     * @param theNum
     * @param diff
     * @param message
     */
    public static Date isDate(String date, String format, String message) {
        if (date == null || date == "") {
            throw new BusinessException(message);
        }
        try {
            Date d = new SimpleDateFormat(format).parse(date);
            if (d == null) {
                throw new BusinessException(message);
            }
            return d;
        } catch (ParseException e) {
            throw new BusinessException(message);
        }
    }

    /**
     * 断言是否小于指定数字，否在抛出Bussiness异常
     * 
     * @param theNum
     * @param diff
     * @param message
     */
    public static void lt(Integer theNum, Integer diff, String message) {
        if (theNum == null || diff == null) {
            throw new BusinessException(message);
        }
        if (theNum >= diff) {
            throw new BusinessException(message);
        }
    }

    /**
     * 断言是否小于等于指定数字，否在抛出Bussiness异常
     * 
     * @param theNum
     * @param diff
     * @param message
     */
    public static void le(Integer theNum, Integer diff, String message) {
        if (theNum == null || diff == null) {
            throw new BusinessException(message);
        }
        if (theNum > diff) {
            throw new BusinessException(message);
        }
    }

    /**
     * 断言是否大于指定数字，否在抛出Bussiness异常
     * 
     * @param theNum
     * @param diff
     * @param message
     */
    public static void gt(Integer theNum, Integer diff, String message) {
        if (theNum == null || diff == null) {
            throw new BusinessException(message);
        }
        if (theNum <= diff) {
            throw new BusinessException(message);
        }
    }

    /**
     * 断言是否大于等于指定数字，否在抛出Bussiness异常
     * 
     * @param theNum
     * @param diff
     * @param message
     */
    public static void ge(Integer theNum, Integer diff, String message) {
        if (theNum == null || diff == null) {
            throw new BusinessException(message);
        }
        if (theNum < diff) {
            throw new BusinessException(message);
        }
    }

    /**
     * 断言是否小于指定数字，否在抛出Bussiness异常
     * 
     * @param theNum
     * @param diff
     * @param message
     */
    public static void lt(Long theNum, Long diff, String message) {
        if (theNum == null || diff == null) {
            throw new BusinessException(message);
        }
        if (theNum >= diff) {
            throw new BusinessException(message);
        }
    }

    /**
     * 断言是否小于等于指定数字，否在抛出Bussiness异常
     * 
     * @param theNum
     * @param diff
     * @param message
     */
    public static void le(Long theNum, Long diff, String message) {
        if (theNum == null || diff == null) {
            throw new BusinessException(message);
        }
        if (theNum > diff) {
            throw new BusinessException(message);
        }
    }

    /**
     * 断言是否大于指定数字，否在抛出Bussiness异常
     * 
     * @param theNum
     * @param diff
     * @param message
     */
    public static void gt(Long theNum, Long diff, String message) {
        if (theNum == null || diff == null) {
            throw new BusinessException(message);
        }
        if (theNum <= diff) {
            throw new BusinessException(message);
        }
    }

    /**
     * 断言是否大于等于指定数字，否在抛出Bussiness异常
     * 
     * @param theNum
     * @param diff
     * @param message
     */
    public static void ge(Long theNum, Long diff, String message) {
        if (theNum == null || diff == null) {
            throw new BusinessException(message);
        }
        if (theNum < diff) {
            throw new BusinessException(message);
        }
    }

    /**
     * 断言是否包含在指定集合中，否在抛出Bussiness异常
     * 
     * @param obj
     *            值
     * @param expectList
     *            期望指定的集合
     * @param message
     *            异常描述
     */
    public static void contain(Object obj, List<Object> expectList, String message) {
        if (obj == null || expectList == null) {
            throw new BusinessException(message);
        }
        if (!expectList.contains(obj)) {
            throw new BusinessException(message);
        }
    }

    /**
     * 断言是否包含在指定数组中，否在抛出Bussiness异常
     * 
     * @param obj
     *            值
     * @param expectArr
     *            期望指定的数组
     * @param message
     *            异常描述
     */
    public static void contain(Object obj, Object[] expectArr, String message) {
        if (obj == null || expectArr == null) {
            throw new BusinessException(message);
        }
        List<Object> expectList = Arrays.asList(expectArr);
        expectArr = null;
        if (!expectList.contains(obj)) {
            throw new BusinessException(message);
        }
    }

}
